sql面试题:面试常考的sql语句题汇总(面试SQL必考必看)

您所在的位置:网站首页 sql server基础面试题 sql面试题:面试常考的sql语句题汇总(面试SQL必考必看)

sql面试题:面试常考的sql语句题汇总(面试SQL必考必看)

2023-07-30 07:35| 来源: 网络整理| 查看: 265

我自己编辑总结的sql面试题目大全,也是每条都验证过的

第一类:sql面试题(学生表_课程表_成绩表_教师表) 

表结构,节选自:http://www.cnblogs.com/qixuejia/p/3637735.html 题目一,节选,自:https://wenku.baidu.com/view/cda288f1b90d6c85ed3ac671.html 题目二,节选,自:http://www.cnblogs.com/qixuejia/p/3637735.html (为了满足“题目”查询条件,在原文的基础上,插入的测试语句中”增加了几条sc,新增了条Course,修改了student的部分年龄)

建议使用在线数据库调试:http://sqlfiddle.com/,非常方便!!

选择数据库类型为SQL Server把建表语句和插入的语句都放入 “Build Schema” Build 一下即成功建立数据库表再到 “Run SQL” 中执行查询语句即可,非常方便

表架构:

Student(S#,Sname,Sage,Ssex) 学生表  Course(C#,Cname,T#) 课程表  SC(S#,C#,score) 成绩表  Teacher(T#,Tname) 教师表

建表语句:

CREATE TABLE student ( s# INT, sname nvarchar(32), sage INT, ssex nvarchar(8) ) CREATE TABLE course ( c# INT, cname nvarchar(32), t# INT ) CREATE TABLE sc ( s# INT, c# INT, score INT ) CREATE TABLE teacher ( t# INT, tname nvarchar(16) )

插入测试数据语句:

insert into Student select 1,N'刘一',20,N'男' union all select 2,N'钱二',19,N'女' union all select 3,N'张三',18,N'男' union all select 4,N'李四',18,N'女' union all select 5,N'王五',17,N'男' union all select 6,N'赵六',19,N'女' insert into Teacher select 1,N'叶平' union all select 2,N'贺高' union all select 3,N'杨艳' union all select 4,N'周磊' insert into Course select 1,N'语文',1 union all select 2,N'数学1',2 union all select 22,N'数学2',2 union all select 3,N'英语',3 union all select 4,N'物理',4 insert into SC select 1,1,56 union all select 1,2,78 union all select 1,3,67 union all select 1,4,58 union all select 2,1,79 union all select 2,2,81 union all select 2,3,92 union all select 2,4,68 union all select 3,1,91 union all select 3,2,47 union all select 3,3,88 union all select 3,4,56 union all select 4,2,88 union all select 4,3,90 union all select 4,4,93 union all select 5,1,46 union all select 5,3,78 union all select 5,4,53 union all select 5,22,53 union all select 6,22,35 union all select 6,1,35 union all select 6,2,68 union all select 6,4,71

 题目一:

1  .统计有学生选修的课程门数。

2  .求选修   C4 课程的学生的平均年龄。

3  . 求  LIU 老师所授课程的每门课程的学生平均成绩。

4  .统计每门课程的学生选修人数     (超过 2 人的课程才统计) 。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列。

5  . 检索学号比   WANG    同学大,而年龄比他小的学生姓名。

6  .检索姓名以    WANG    打头的所有学生的姓名和年龄。

7  .在 SC 中检索成绩为空值的学生学号和课程号。

8  . 求年龄大于女同学平均年龄的男学生姓名和年龄。

9  .求年龄大于所有女同学年龄的男学生姓名和年龄。

其中涉及单表题:    1.4.6.7

我自己做的答案(倒序排列的,用换行分隔每个题),都是对的 (标准答案:https://wenku.baidu.com/view/cda288f1b90d6c85ed3ac671.html):

--9 select s1.sname,s1.sage from student s1 where s1.ssex=N'男' and s1.sage>all(select sage from student where ssex=N'女') --8 select s1.sname,s1.sage from student s1 where s1.ssex=N'男' and s1.sage>(select avg(sage) from student where ssex=N'女') --7 select s#,c# from sc where score is null --6 select sname,sage from student where sname like N'李%' --5 --方法一 select sname from student where s#>(select s# from student where sname=N'李四') and sages2.s# and s1.sage2 order by count(sc.s#) desc,sc.c#; --3 --贺高老师每门课程的学生平均成绩 ----思路:先贺高老师的所有课程,再每组平均成绩 select sc.c#,avg(score) from teacher,course,sc where teacher.t#=course.t# and course.c#=sc.c# --老师找到课程+课程找到成绩=老师找到成绩 and teacher.tname=N'贺高' group by sc.c# --2 --方法一 select avg(Student.sage) from Student,sc where Student.s#=sc.s# and sc.c#=4 --方法二 select avg(Student.sage) from Student where Student.s# in(select sc.s# from sc where Student.s#=sc.s# and sc.c#=4) --1 select count(distinct(c#)) from sc; --查询表中数据 --select * from Student; --select * from course; --select * from sc; --select * from teacher;

题目二:

问题: 1、查询“001”课程比“002”课程成绩高的所有学生的学号; select a.S# from (select s#,score from SC where C#='001') a,(select s#,score from SC where C#='002') b where a.score>b.score and a.s#=b.s#; 2、查询平均成绩大于60分的同学的学号和平均成绩; select S#,avg(score) from sc group by S# having avg(score) >60; 3、查询所有同学的学号、姓名、选课数、总成绩; select Student.S#,Student.Sname,count(SC.C#),sum(score) from Student left Outer join SC on Student.S#=SC.S# group by Student.S#,Sname 4、查询姓“李”的老师的个数; select count(distinct(Tname)) from Teacher where Tname like '李%'; 5、查询没学过“叶平”老师课的同学的学号、姓名; select Student.S#,Student.Sname from Student where S# not in (select distinct( SC.S#) from SC,Course,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname='叶平'); 6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; select Student.S#,Student.Sname from Student,SC where Student.S#=SC.S# and SC.C#='001'and exists( Select * from SC as SC_2 where SC_2.S#=SC.S# and SC_2.C#='002'); 7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; select S#,Sname from Student where S# in (select S# from SC ,Course ,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname='叶平' group by S# having count(SC.C#)=(select count(C#) from Course,Teacher where Teacher.T#=Course.T# and Tname='叶平')); 8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名; Select S#,Sname from (select Student.S#,Student.Sname,score ,(select score from SC SC_2 where SC_2.S#=Student.S# and SC_2.C#='002') score2 from Student,SC where Student.S#=SC.S# and C#='001') S_2 where score2 60); 10、查询没有学全所有课的同学的学号、姓名; select Student.S#,Student.Sname from Student,SC where Student.S#=SC.S# group by Student.S#,Student.Sname having count(C#) =60 THEN 1 ELSE 0 END)/COUNT(*) AS 及格百分数 FROM SC T,Course where t.C#=course.C# GROUP BY t.C# ORDER BY 100 * SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) DESC 20、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(001),马克思(002),OO&UML (003),数据库(004) SELECT SUM(CASE WHEN C# ='001' THEN score ELSE 0 END)/SUM(CASE C# WHEN '001' THEN 1 ELSE 0 END) AS 企业管理平均分 ,100 * SUM(CASE WHEN C# = '001' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = '001' THEN 1 ELSE 0 END) AS 企业管理及格百分数 ,SUM(CASE WHEN C# = '002' THEN score ELSE 0 END)/SUM(CASE C# WHEN '002' THEN 1 ELSE 0 END) AS 马克思平均分 ,100 * SUM(CASE WHEN C# = '002' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = '002' THEN 1 ELSE 0 END) AS 马克思及格百分数 ,SUM(CASE WHEN C# = '003' THEN score ELSE 0 END)/SUM(CASE C# WHEN '003' THEN 1 ELSE 0 END) AS UML平均分 ,100 * SUM(CASE WHEN C# = '003' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = '003' THEN 1 ELSE 0 END) AS UML及格百分数 ,SUM(CASE WHEN C# = '004' THEN score ELSE 0 END)/SUM(CASE C# WHEN '004' THEN 1 ELSE 0 END) AS 数据库平均分 ,100 * SUM(CASE WHEN C# = '004' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = '004' THEN 1 ELSE 0 END) AS 数据库及格百分数 FROM SC

 

第二类:部门工资前三高的员工、部门工资最高的员工

表结构,节选自:https://www.cnblogs.com/an5456/p/10478949.html 题目,节选自:https://www.cnblogs.com/an5456/p/10478949.html答案灵感来自于: 《sql分组(orderBy、GroupBy)获取每组前一(几)条数据》的“5、根据Name分组取最大的两个(N个)Val” 。 地址:https://www.cnblogs.com/linJie1930906722/p/5983159.html 也可以查看我转载的博客:《sql分组(orderBy、GroupBy)获取每组前一(几)条数据》

(为了满足“题目”查询条件,在原文的基础上,插入的测试语句中”增加了几条Employee,修改了Employee的部分薪资)

建议使用在线数据库调试:http://sqlfiddle.com/,非常方便!!

选择数据库类型为MySQL把建表语句和插入的语句都放入 “Build Schema” Build 一下即成功建立数据库表再到 “Run SQL” 中执行查询语句即可,非常方便 Create table If Not Exists Employee (Id int, Name varchar(255), Salary int, DepartmentId int); Create table If Not Exists Department (Id int, Name varchar(255)); Truncate table Employee; insert into Employee (Id, Name, Salary,DepartmentId) values ('1', 'Joe', '70000', '1'); insert into Employee (Id, Name, Salary,DepartmentId) values ('2', 'Henry', '85000', '2'); insert into Employee (Id, Name, Salary,DepartmentId) values ('3', 'Sam', '60000', '2'); insert into Employee (Id, Name, Salary,DepartmentId) values ('4', 'Max', '90000', '1'); insert into Employee (Id, Name, Salary,DepartmentId) values ('6', 'Henry2', '85000', '2'); insert into Employee (Id, Name, Salary,DepartmentId) values ('7', 'Henry3', '83000', '2'); insert into Employee (Id, Name, Salary,DepartmentId) values ('8', 'Randy2', '87000', '1'); insert into Employee (Id, Name, Salary,DepartmentId) values ('5', 'Randy', '85000', '1'); Truncate table Department; insert into Department (Id, Name) values('1', 'IT'); insert into Department (Id, Name) values('2', 'Sales');

1.  每个部门工资最高的员工(如果有两个薪资相同的会显示出来) (不明白“相关子查询”的朋友请看:https://blog.csdn.net/HD243608836/article/details/88832509):

select * FROM Employee e WHERE e.Salary = ( select max(Salary) from Employee where DepartmentId=e.DepartmentId) order by e.DepartmentId; --select * from Employee;

结果:

IdNameSalaryDepartmentId4Max9000012Henry8500026Henry2850002

 

2.1  求每个部门的最高工资(不显示重复的薪资)

select DepartmentId,max(Salary) from Employee group by DepartmentId; --select * from Employee;

 结果:

departmentIdmax(salary)190000285000

 

2.2  求每个部门的最高工资(显示重复的薪资) (就是把题目“1”的select的显示条件从星号“*”换成了具体字段)

select e.DepartmentId,e.Salary FROM Employee e WHERE e.Salary = ( select max(Salary) from Employee where DepartmentId=e.DepartmentId) order by e.DepartmentId; --select * from Employee;

 结果:

departmentIdsalary190000285000285000

3.  每个部门工资前三高的员工 (用到了“相关子查询”的知识,不明白的请参看:https://blog.csdn.net/HD243608836/article/details/88832509, 不然保证你看不懂!!)

方法一(必考重点):

select e.* from Employee e where 3>( select count(*) from Employee where DepartmentId=e.DepartmentId and Salary>e.Salary) order by DepartmentId,Salary; 语句构造解析: 表结构: |Id | Name | Salary |DepartmentId| +——————————————————————————————————+ |'1'| 'Joe' | '70000'| '1' |(第一条) |'2'| 'Henry'| '85000'| '2' |(第二条)…… ……以此类推…… 因为语句中,子查询中使用到了父查询的字段,所以一定是“相关子查询”!! 1.先取得数据库中的第一个元组(即第一行数据),把第一个元组中的字段作为参数,传入子查询中 select count(*) from Employee where DepartmentId=1 and Salary>70000; -->返回结果为3,where后的条件“3>3”返回false,则该条元组不进入汇总表。 (PS:返回0个证明是工资第一高,1个是第二高,2个是第三高……) 2.再取得数据库中的第二个元组 select count(*) from Employee where DepartmentId=2 and Salary>85000; -->返回结果为1,where后的条件“3>1”返回true,则该条元组进入汇总表。 ……以此类推…… 最后,返回汇总表。

结果:

IdNameSalaryDepartmentId5Randy8500018Randy28700014Max9000017Henry38300022Henry8500026Henry2850002

方法二:

(和方法一差不多,也是“相关子查询”,只不过用了exists和having)

select e.* from Employee e where exists ( select count(*) from Employee where DepartmentId=e.DepartmentId and Salary>e.Salary having count(*)


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3